#### Full AMCE Tables ####

## Table S2: Unconditional AMCE ##
fig2_dat = import(here("data","fig2_dat.rds"))

tab2 = fig2_dat |> 
  mutate(effect = round(effect, 2),
         se = round(se, 4)) |> 
  select(label, effect, se, N_obs) |> 
  arrange(desc(effect)) |> 
  kable(col.names = c("Policy", "AMCE", "SE", "N"),
        format = 'latex', booktabs = T, longtable = T,
        caption = "Unconditional AMCE") |> 
  kable_classic(full_width = F)

tab2 |> 
  writeLines(here("tables","appendix","tab_s2.tex")) 

## Table S3: AMCE By Income/Education Level ##
fig3_dat = bind_rows(import(here("data","fig3_inc_dat.rds")) |> mutate(demo = "inc"),
                     import(here("data","fig3_edu_dat.rds")) |> mutate(demo = "edu")) |> 
  mutate(hilo = ifelse(group == 0, "lo", "hi"),
         demo_full = paste(demo, hilo, sep = "_"),
         est = paste0(round(effect, 2),"\nSE = ",
                      round(se, 4), "\nN = ",
                      N_obs) |> linebreak()) |> 
  pivot_wider(id_cols = label, names_from = demo_full, values_from = est) |> 
  arrange(label) |> 
  select(label, inc_lo, inc_hi, edu_lo, edu_hi)

tab3 = fig3_dat |> 
  kable(col.names = c("Policy","Low","High","Low","High"),
        format = 'latex', booktabs = T, longtable = T,
        caption = "AMCE by Income/Education") |> 
  kable_classic(full_width = F) |> 
  add_header_above(c(" " = 1, "Income" = 2, "Education" = 2))

tab3 |> 
  fix_table() |> 
  writeLines(here("tables","appendix","tab_s3.tex")) 

## Table S4: AMCE for Income by Agreement
fig4_dat = bind_rows(import(here("data","fig4_inc_agree_dat.rds")) |> mutate(agree = "agree"),
                     import(here("data","fig4_inc_disagree_dat.rds")) |> mutate(agree = "disagree")) |> 
  mutate(hilo = ifelse(group == 0, "lo", "hi"),
         demo_full = paste(agree, hilo, sep = "_"),
         est = paste0(round(effect, 2),"\nSE = ",
                      round(se, 4), "\nN = ",
                      N_obs) |> linebreak()) |> 
  pivot_wider(id_cols = label, names_from = demo_full, values_from = est) |> 
  arrange(label) |> 
  select(label, agree_lo, agree_hi, disagree_lo, disagree_hi)

tab4 = fig4_dat |> 
  kable(col.names = c("Policy","Low","High","Low","High"),
        format = 'latex', booktabs = T, longtable = T,
        caption = "AMCE for Income by Agreement") |> 
  kable_classic(full_width = F) |> 
  add_header_above(c(" " = 1, "Agree" = 2, "Disagree" = 2))

tab4 |> 
  fix_table() |> 
  writeLines(here("tables","appendix","tab_s4.tex"))

## Table S5: AMCE for Education by Agreement
fig6_dat = bind_rows(import(here("data","fig6_edu_agree_dat.rds")) |> mutate(agree = "agree"),
                     import(here("data","fig6_edu_disagree_dat.rds")) |> mutate(agree = "disagree")) |> 
  mutate(hilo = ifelse(group == 0, "lo", "hi"),
         demo_full = paste(agree, hilo, sep = "_"),
         est = paste0(round(effect, 2),"\nSE = ",
                      round(se, 4), "\nN = ",
                      N_obs) |> linebreak()) |> 
  pivot_wider(id_cols = label, names_from = demo_full, values_from = est) |> 
  arrange(label) |> 
  select(label, agree_lo, agree_hi, disagree_lo, disagree_hi)

tab5 = fig6_dat |> 
  kable(col.names = c("Policy","Low","High","Low","High"),
        format = 'latex', booktabs = T, longtable = T,
        caption = "AMCE for Education by Agreement") |> 
  kable_classic(full_width = F) |> 
  add_header_above(c(" " = 1, "Agree" = 2, "Disagree" = 2))

tab5 |> 
  fix_table() |> 
  writeLines(here("tables","appendix","tab_s5.tex"))

## Table S6: AMCE for Education by Agreement
fig8_dat = bind_rows(import(here("data","fig8_r_edu_agree_dat.rds")) |> mutate(agree = "agree"),
                     import(here("data","fig8_r_edu_disagree_dat.rds")) |> mutate(agree = "disagree")) |> 
  mutate(hilo = ifelse(group == 0, "lo", "hi"),
         demo_full = paste(agree, hilo, sep = "_"),
         est = paste0(round(effect, 2),"\nSE = ",
                      round(se, 4), "\nN = ",
                      N_obs) |> linebreak()) |> 
  pivot_wider(id_cols = label, names_from = demo_full, values_from = est) |> 
  arrange(label) |> 
  select(label, agree_lo, agree_hi, disagree_lo, disagree_hi)

tab6 = fig8_dat |> 
  kable(col.names = c("Policy","Low","High","Low","High"),
        format = 'latex', booktabs = T, longtable = T,
        caption = "AMCE for Education by Agreement (Republicans)") |> 
  kable_classic(full_width = F) |> 
  add_header_above(c(" " = 1, "Agree" = 2, "Disagree" = 2))

tab6 |> 
  fix_table() |> 
  writeLines(here("tables","appendix","tab_s6.tex"))

## Table S7: Full differences in revealed importance by income and education 

fig4_dat = bind_rows(import(here("data","fig4_inc_agree_dat.rds")) |> 
                       mutate(agree = "Agree"),
                     import(here("data","fig4_inc_disagree_dat.rds")) |> 
                       mutate(agree = "Disagree")) |> 
  invert_group()

fig6_dat = bind_rows(import(here("data","fig6_edu_agree_dat.rds")) |> 
                       mutate(agree = "Agree"),
                     import(here("data","fig6_edu_disagree_dat.rds")) |> 
                       mutate(agree = "Disagree")) |>
  invert_group()

inc_agree = left_join(fig4_dat %>% filter(group==1 & agree == "Agree"),
                      fig4_dat %>% filter(group==0 & agree == "Agree"),
                      by="condition") |> 
  mutate(inc_agree_diff = round(effect.x - effect.y,2),
         inc_agree_ci_lo = round(-1*ci_int_high.x,2), # -1 x since interaction was calculated w/ opposite groupings
         inc_agree_ci_hi = round(-1*ci_int_low.x,2),
         inc_agree = paste0(inc_agree_diff,"\n(",
                            inc_agree_ci_lo,", ",inc_agree_ci_hi,")\nN = ",
                            N_obs.x) |> 
           linebreak()
  ) |> 
  select(condition, inc_agree_diff, inc_agree)

inc_disagree = left_join(fig4_dat %>% filter(group==1 & agree == "Disagree"),
                         fig4_dat %>% filter(group==0 & agree == "Disagree"),
                         by="condition") |> 
  mutate(inc_disagree_diff = round(effect.x - effect.y,2),
         inc_disagree_ci_lo = round(-1*ci_int_high.x,2),
         inc_disagree_ci_hi = round(-1*ci_int_low.x,2),
         inc_disagree = paste0(inc_disagree_diff,"\n(",
                               inc_disagree_ci_lo,", ",inc_disagree_ci_hi,")\nN = ",
                               N_obs.x) |> 
           linebreak()) |> 
  select(condition, inc_disagree_diff, inc_disagree)

edu_agree = left_join(fig6_dat %>% filter(group==1 & agree == "Agree"),
                      fig6_dat %>% filter(group==0 & agree == "Agree"),
                      by="condition") |> 
  mutate(edu_agree_diff = round(effect.x - effect.y,2),
         edu_agree_ci_lo = round(-1*ci_int_high.x,2),
         edu_agree_ci_hi = round(-1*ci_int_low.x,2),
         edu_agree = paste0(edu_agree_diff,"\n(",
                            edu_agree_ci_lo,", ",edu_agree_ci_hi,")\nN = ",
                            N_obs.x) |> 
           linebreak()) |> 
  select(condition, edu_agree_diff, edu_agree)

edu_disagree = left_join(fig6_dat %>% filter(group==1 & agree == "Disagree"),
                         fig6_dat %>% filter(group==0 & agree == "Disagree"),
                         by="condition") |> 
  mutate(edu_disagree_diff = round(effect.x - effect.y,2),
         edu_disagree_ci_lo = round(-1*ci_int_high.x,2),
         edu_disagree_ci_hi = round(-1*ci_int_low.x,2),
         edu_disagree = paste0(edu_disagree_diff,"\n(",
                               edu_disagree_ci_lo,", ",edu_disagree_ci_hi,")\nN = ",
                               N_obs.x) |> 
           linebreak()) |> 
  select(condition, edu_disagree_diff, edu_disagree)

tab_df_full = inc_agree |> 
  left_join(inc_disagree) |>
  left_join(edu_agree) |> 
  left_join(edu_disagree) |> 
  left_join(policies |> 
              select(label, newlabel),
            join_by(condition == label)) |> 
  arrange(newlabel) |> 
  select(newlabel, inc_agree, inc_disagree, edu_agree, edu_disagree) |> 
  kable(col.names = c("Policy","Agree","Disagree",
                      "Agree","Disagree"),
        format = 'latex', booktabs = T, longtable = T,
        caption = "Full differences in revealed importance by income and education") |> 
  kable_classic(full_width = F) |> 
  add_header_above(c(" " = 1, "Income" = 3, "Education" = 3))

tab_df_full |> 
  fix_table() |> 
  writeLines(here("tables","appendix","tab_s7.tex"))

ccat("See 'tab_s2.tex' to 'tab_s7.tex' for Tables S2-7 output (Appendix B)")